package com.ruoyi.user.service.impl;

import com.github.wxpay.sdk.WXPayUtil;
import com.ruoyi.common.core.cg.result.Result;
import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.user.service.MyWxService;
import com.ruoyi.user.service.OrderService;
import com.ruoyi.user.util.HttpClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

@Service
public class MyWxServiceImpl implements MyWxService {
    private final String APPID = "wx0c57f6714a5f52ac";
    private final String SECRET = "6c93b6954b476a4c501b21a94a1f2a97";
    @Autowired
    private OrderService orderService;
    @Override
    public Result getAccessToken() throws Exception{
        String urlStr = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+APPID+"&secret="+SECRET;
        URL url = new URL(urlStr);
        // 打开链接
        HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
        httpURLConnection.setRequestMethod("GET");
        // 获取响应码
        int responseCode = httpURLConnection.getResponseCode();
        if (responseCode == HttpURLConnection.HTTP_OK) {
            // 读取响应内容
            BufferedReader reader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
            StringBuilder response = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                response.append(line);
            }
            reader.close();

            // 解析 JSON 响应
            String responseJson = response.toString();
            if (responseJson.contains("access_token")) {
                int startIndex = responseJson.indexOf("\"access_token\":\"") + "\"access_token\":\"".length();
                int endIndex = responseJson.indexOf("\"", startIndex);
                return new Result(true,"获取accessToken成功",responseJson.substring(startIndex, endIndex));
            }
        }
        return null;
    }

    @Override
    public Result generateCodeUrl(String onumber) throws Exception{
        Map map = new HashMap();
        map.put("appid","wxab8acb865bb1637e");
        map.put("mch_id","11473623");
        map.put("body","拼团订单");
        map.put("out_trade_no",onumber);
        map.put("total_fee","1");
        map.put("spbill_create_ip","127.0.0.1");
        map.put("notify_url","127.0.0.1");
        map.put("trade_type","NATIVE");
        map.put("nonce_str", WXPayUtil.generateNonceStr());
        String xmlParam = WXPayUtil.generateSignedXml(map,"2ab9071b06b9f739b950ddb41db2690d");
        HttpClient httpClient = new HttpClient("https://api.mch.weixin.qq.com/pay/unifiedorder");
        httpClient.setXmlParam(xmlParam);
        httpClient.isHttps();
        httpClient.post();
        String result = httpClient.getContent();
        Map<String, String> data = WXPayUtil.xmlToMap(result);
        String codeUrl = data.get("code_url");
        if(codeUrl == null){
            orderService.updatePayment(onumber);
        }
        return new Result(true,"生成二维码成功",codeUrl);
    }
}
